﻿using Newtonsoft.Json;
using System.Text;
using System.Text.Json.Serialization;

namespace doctor_probe1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private async void button1_Click(object sender, EventArgs e)
        {
            // GET请求
            using (HttpClient client = new HttpClient())
            {
                try
                {

                    string url = "http://localhost:6969/api/users";

                    HttpResponseMessage response = await client.GetAsync(url);
                    response.EnsureSuccessStatusCode();

                    string responseBody = await response.Content.ReadAsStringAsync();
                    MessageBox.Show(responseBody, "Response");
                }
                catch (Exception ex)
                {
                    MessageBox.Show($"Error: {ex.Message}", "Error");
                }
            }
        }

        private async void button2_Click(object sender, EventArgs e)
        {

            using (HttpClient client = new HttpClient())
            {
                // POST请求
                try
                {
                    string url = "http://localhost:6969/api/user/register";
                    var requestBody = new { name = "foo", password = "bar" };
                    string json = JsonConvert.SerializeObject(requestBody);
                    StringContent content = new StringContent(json, Encoding.UTF8, "application/json");
                    HttpResponseMessage response = await client.PostAsync(url, content);
                    response.EnsureSuccessStatusCode();
                    string responseBody = await response.Content.ReadAsStringAsync();
                    MessageBox.Show(responseBody, "Response");
                }
                catch (Exception ex)
                {
                    MessageBox.Show($"Error: {ex.Message}", "Error");
                }
            }
        }
    }
}
